// app.js
import Cache from './utils/Cache'
import Http from './utils/api'
import Route from './utils/util'
App({
  onLaunch(res){
      this.login()
      if (!Cache.has('token')) {
        if (!Cache.get('apitoken')) {
          this.login();
        }else{
          
        }
      }
  },
  login(){
    Http.Http(
      Route.apilogin,
      {
        username:'zhangsan',
        password:'admin6666'  
      },
      'POST'
    ).then(res=>{
      Cache.set('apitoken',res.data);
      if(!Cache.get('jwttoken')){
        this.silentlogin()
      }
      
    })
  },
  silentlogin(){
    wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
      title: '加载中',
      icon: 'loading',
    });
    setTimeout(() => {
      wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // console.log(res.code);
        let code = res.code
        Http.Http(
          Route.silentlogin,
          {
            code:code
          },
          'POST',
          Cache.get('apitoken')
        ).then(res=>{
          // console.log(res.data);
          Cache.forever('token',res.data)
          wx.hideLoading();
        })
      }
    })
    }, 1000)
    
    
  }
})
